﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using SimpleQueueingSystem.Interfaces;

namespace SimpleQueueingSystem.Invoker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1Load(object sender, EventArgs e)
        {
            txtQueuePath.Text = @".\Private$\SimpleQueue";
            txtCounterNo.Text = "1";
        }

        private void btnInvokeClick(object sender, EventArgs e)
        {
            MessageQueue queue = new MessageQueue(txtQueuePath.Text);
            try
            {

                queue.Formatter = new BinaryMessageFormatter();

                System.Messaging.Message message = queue.Receive(TimeSpan.FromMilliseconds(500));
                if (message != null)
                {
                    IRequest currentRequest = (IRequest)message.Body;
                    lblProcessing.Text = string.Format("{0} at Counter:{1}", currentRequest.GetTicketInformation(),
                                                       txtCounterNo.Text);
                    currentRequest.Process();
                }
                else
                {
                    lblProcessing.Text = "";
                }

            }
            catch (MessageQueueException ex)
            {
                //ignore the exception
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            finally
            {
                queue.Close();
            }
        }
    }
}
